import java.util.Random;
import java.util.Scanner;
public class test {
    public static void main(String[] args) {  //密码系统测试
        System.out.println("=========================");
        System.out.println("欢迎使用密码管理系统");
        System.out.println("=========================");
        System.out.println("请选择操作：");
        System.out.println("1.加密");
        System.out.println("2.解密");
        System.out.println("3.判断密码强度");
        System.out.println("4.密码生成");
        System.out.println("5.退出");
        Scanner sc = new Scanner(System.in);
        while(true){
            System.out.print("请输入选项序号：");
            int active = sc.nextInt();
            switch (active){
                case 1:
                    System.out.print("请输入你要加密的密码：");
                    String s = sc.next();
                    System.out.println("加密后的密码是："+jiami(s));
                    break;
                case 2:
                    System.out.print("请输入你要解密的密码：");
                    String s_1 = sc.next();
                    System.out.println("解密后的密码是："+jiemi(s_1));
                    break;
                case 3:
                    System.out.print("请输入你要判断密码强度的密码：");
                    String s_2 = sc.next();
                    panduan(s_2);
                    break;
                case 4:
                    String s_3 = shenchen();
                    System.out.println("生成一个12位的密码强度高的密码"+s_3);
                    break;
                case 5:
                    System.exit(0);
            }
        }
    }


    public static String pianyi(String input){   //进行偏移处理，偏移值为3
        String s = new String();
        for (int i = 0; i < input.length();i++) {
            char c = input.charAt(i);
            char num = (char) (c+3);  //对照ascll码，对密码每个进行偏移
            s += num;
        }
        return s;
    }
    public static String diaoshu(String input){  //将第一个数和最后一个数字颠倒
        StringBuilder sb = new StringBuilder(input);
        char c = sb.charAt(0);               //获取原密码的第一个数字
        char ch = sb.charAt(sb.length()-1);  //获取原密码的最后一个数字
        String s = new String();
        for (int i = 0; i < sb.length(); i++) {   //通过判断密码的位置来将原秘密加入到新的字符串中
            if(i == 0){
                s += ch;
            } else if (i == sb.length()-1) {
                s += c;
            }else{
                s += sb.charAt(i);
            }
        }
        return s;
    }

    public static String jiami(String input){   //加密的功能
        String s1 = pianyi(input);     //进行偏移
        String s2 = diaoshu(s1);       //进行第一个数字和第二数字调换
        StringBuilder sb = new StringBuilder(s2);
        sb.reverse();                  //将密码反转
        String s = new String(sb);
        return s;
    }

    public static String huanyuan(String input){   //进行还原偏移处理，偏移值为3
        String s = new String();
        for(int i = 0;i < input.length();i++){
            char c = input.charAt(i);
            char num = (char) (c-3);  //对照ascll码，对秘密每个字还原
            s += num;
        }
        return s;
    }

    public static String jiemi(String input){   //解密的功能
        String s = new String();
        StringBuilder sb = new StringBuilder(input);
        sb.reverse();               //将密码进行反转
        s = sb.toString();
        String s1 = diaoshu(s);     //对密码进行调转第一个数和最后一个数
        String s2 = huanyuan(s1);   //对密码进行偏移还原
        return s2;

    }
    public static void panduan(String input){   //判断密码强度
        int num = 0;
        int daxie = 0;
        int xiaoxie = 0;

        for(int i = 0;i<input.length();i++){
            if(65 <= input.charAt(i)&&input.charAt(i) <=90){
                daxie = 1;
            } else if (97 <= input.charAt(i) && input.charAt(i)<=112) {
                xiaoxie = 1;
            } else if (48 <= input.charAt(i)&&input.charAt(i) <= 57) {
                num = 1;
            }
        }
        int all = num + daxie +xiaoxie;
        if(num == 1 && input.length() >= 8 && daxie == 0 && xiaoxie ==0 || input.length() < 8){
            System.out.println("密码强度较低");
        }
        if(num == 1 && all == 2 && input.length() >= 8){
            System.out.println("密码强度中等");
        }
        if(num == 1 && input.length() >= 8 && daxie == 1 && xiaoxie ==1){
            System.out.println("密码强度高");
        }
    }

    public static String shenchen(){         //随机生成一个12位的密码强度高的密码
        String s = new String();
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for(int i = 0;i<6;i++){
            int num = random.nextInt(10);
            sb.append(num);
        }
        for(int i = 0;i<3;i++){
            int daxie = random.nextInt(26) + 65;
            char c =(char) daxie;
            sb.append(c);
        }
        for(int i = 0;i<3;i++){
            int xiaoxie = random.nextInt(26) + 97;
            char c = (char) xiaoxie;
            sb.append(c);
        }
        return s = sb.toString();
    }

}
